package org.test.client.listener;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JComponent;

import org.test.client.ui.util.UIConstants;
import org.test.debug.Debug;

public class CommonMouseListener extends MouseAdapter{
	protected JComponent comp = null;
	protected List<ClickAction> actions;
	protected List<EnteredAction> enteredActions;
	
	public CommonMouseListener(JComponent comp){
		this.comp = comp;
	}
	
	public CommonMouseListener(JComponent comp,List<ClickAction> actions) {
		this.comp = comp;
		this.actions = actions;
	}
	
	public CommonMouseListener(JComponent comp, ClickAction aAc) {
		this.comp = comp;
		this.actions = new LinkedList<ClickAction>();
		this.actions.add(aAc);
	}

	
	public CommonMouseListener(JComponent comp, EnteredAction eAc) {
		this.comp = comp;
		this.enteredActions = new LinkedList<EnteredAction>();
		this.enteredActions.add(eAc);
	}
	
	public CommonMouseListener(JComponent comp, ClickAction aAc,EnteredAction eAc) {
		this.comp = comp;
		this.actions = new LinkedList<ClickAction>();
		this.actions.add(aAc);
		this.enteredActions = new LinkedList<EnteredAction>();
		this.enteredActions.add(eAc);
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		comp.setCursor(UIConstants.handCursor);
		execEnteredActions(e,true);
	}
	
	@Override
	public void mouseExited(MouseEvent e) {
		comp.setCursor(UIConstants.defaultCursor);
		execEnteredActions(e,false);
	} 
	
	@Override
	public void mouseClicked(MouseEvent mouseevent) {
		execActions(mouseevent);
	}
	
	@Override
	public void mousePressed(MouseEvent mouseevent) {
		super.mousePressed(mouseevent);
	}
	
	public void execActions(MouseEvent mouseevent){
		if(actions != null && actions.size()>0){
			for(ClickAction a : actions){
				a.doOnClick(mouseevent);
			}
		}else{
//			Debug.debug("Click Action is null");
		}
	}
	
	public void execEnteredActions(MouseEvent mouseevent,boolean isEnter){
			
		if(enteredActions != null && enteredActions.size()>0){
			for(EnteredAction a : enteredActions){
				if(isEnter){
					a.doOnEntered(mouseevent);
				}else{
					a.doOnExited(mouseevent);
				}
			}
		}else{
//			Debug.debug("Enter Action is null");
		}
	}
	
	public void addClickAction(ClickAction ca){
		if(actions == null){
			actions = new LinkedList<ClickAction>();
		}
		actions.add(ca);
	}
	
	public void addEnteredAction(EnteredAction ea){
		if(enteredActions == null){
			enteredActions = new LinkedList<EnteredAction>();
		}
		enteredActions.add(ea);
	}
}
